package love.lemoncat.junit5.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Controller
@RequestMapping("/example")
public class ExampleController {

    @ResponseBody
    @RequestMapping("/json")
    public Hello json(String name) {
        return new Hello("001", name);
    }

    @ResponseBody

    @RequestMapping("/json/requestbody")
    public Hello json(@RequestBody Hello hello) {
        return hello;
    }

    @RequestMapping("/view")
    public String view(ModelMap modelMap, String name) {
        modelMap.put("name", name);
        return "example/" + name;
    }

    @PostMapping("/upload")
    @ResponseBody
    public Object fileUpload(ModelMap modelMap, @RequestParam(value = "file") MultipartFile file, Hello hello) throws IOException {
        modelMap.put("hello", hello);
        log.info("上传的文件：{}", file);
        log.info("对象参数：{}", hello);
        if (file == null || file.isEmpty()) {
            return "上传失败";
        } else {
            log.info("上传的文件名：{},上传的内容：{}", file.getOriginalFilename(), new String(file.getBytes()));
        }
        return "上传成功";
    }


}